﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
namespace WPFExcelReport
{
    //完成移库功能，从CP移入CW，同时进行包装
    /// <summary>
    /// 从CP移入CW，但是不进行WV03
    /// </summary>
    public partial class TWB06
    {  

        public TWB06()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBST();
            this.DataContext = MV;
            InitializeComponent();
        }

       
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
                return;
            //if (app.User.Trim() == "JLTV" || app.User.Trim() == "ITXV")
            {
                STKNR = dwMaster["WBBSTKNR"].ToString();
            }
        } 
       
        public void WB2WH(MessageInfo msg)
        {
            TargetMTR = 0;
            if (msg == null)
                return;
            if (LIEFSCH.Length < 1)
            {
                TBMain.Clear();
                return;
            }
            msg.LO = SLO;
            msg.LB = SLB;
            DataSet SResult;
            SResult = TMV.WB2WH(msg);
            MergeData(SResult, false);
            cvMaster.CustomFilter = "WBBLO='" + SLO + "' AND WBBLB='" + SLB + "'";
        }

        public void MOVE(DataRow _dwWB)
        {
            vwMaster.View.MoveCurrentTo(-1);
            TMV.MOVE(_dwWB, LO, LB, LP, this.RUNID);
            TargetMTR += Helpers.Helper.RoundX(Convert.ToDouble(_dwWB["WBBISTKLG"].ToString()) / 1000, 100);
            vwMaster.View.MoveCurrentToFirst();
        }
        public  void MoveSelectedCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                foreach (DataRow _dwWB in TBMain.Rows)
                {
                    if (_dwWB["WBBLB"].ToString() == LO)
                    {
                        continue;
                    }
                    TMV.MOVE(_dwWB, LO, LB, LP, this.RUNID);
                    TargetMTR += Helpers.Helper.RoundX(Convert.ToDouble(_dwWB["WBBISTKLG"].ToString()) / 1000, 100);
                }
                TMV.VWBW.POSTCHG();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void Free()
        {
            SLO = LO;
            SLB = LB;
            MessageBox.Show("请修改目标库位，并通过选择发货单执行查询");
        }

        public override void DeleteCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            Free();
        }

        private void LIEFSCH_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            this.Focus();
            if (e.Key != System.Windows.Input.Key.Enter)
                return;
            if (e.Key != System.Windows.Input.Key.Return)
                return;
            WB2WH(null);
        }

        private void STKNR_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            this.Focus();
            if (e.Key != System.Windows.Input.Key.Enter)
                return;
            if (e.Key != System.Windows.Input.Key.Return)
                return;
            filter = "WBBSTKNR='" + STKNR + "' AND WBBLO='" + SLO + "' AND WBBLB='" + SLB + "'";
            rows = TBMain.Select(filter);
            if (rows.Length == 0)
            {
                System.Media.SystemSounds.Beep.Play();
                return;
            }
            MOVE(rows[0]);
            System.Media.SystemSounds.Asterisk.Play();
            STKNR = "";
            TextBox txt = sender as TextBox;
            txt.Focus();
        }

        public override void LOADBYAUF()
        {
            TBMain.Clear();
            if (ORDERNUM.Length < 20)
                return;
            LIEFSCH = ORDERNUM.Substring(11, 8);
            PM["AUFTYP"] = ORDERNUM.Substring(0, 1);
            PM["KRS"] = ORDERNUM.Substring(1, 2);
            PM["LFD"] = ORDERNUM.Substring(3, 6);
            PM["POS"] = ORDERNUM.Substring(9, 3);
            PM["LIEFSCH"] = ORDERNUM.Substring(12, 8);
            PM["LO"] = SLO;
            PM["LB"] = SLB;
            SResult = TMV.WB2WH(PM);
            MergeData(SResult, true, true);
            base.LOADBYAUF();
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (ORDERNUM.Length < 20)
                return;
            LIEFSCH = EXAUF.Substring(11, 8);
            PM["AUFTYP"] = EXAUF.Substring(0, 1);
            PM["KRS"] = EXAUF.Substring(1, 2);
            PM["LFD"] = EXAUF.Substring(3, 6);
            PM["POS"] = EXAUF.Substring(9, 3);
            PM["LIEFSCH"] = EXAUF.Substring(12, 8);
            PM["LO"] = SLO;
            PM["LB"] = SLB;
            SResult = TMV.WB2WH(PM);
            MergeData(SResult, true, true);
            base.OnEXAUFChg(vlaue);
        }
    }
}
